#include <config.h>
#include <preferences_dialog.h>
#include <configuration.h>

#include <gtkmm.h>
using sigc::mem_fun;
using Glib::ustring;
#include <glibmm/i18n.h>

#include <list>

#include <iomanip>
#include <iostream>
using std::cerr;
using std::endl;

preferences_dialog::preferences_dialog (configuration &nconfig):
config_ (nconfig)
{
    /* acquire ui from ui file */
    Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file (
            PACKAGE_UI_DIR "/preferences_dialog.glade");

    /* initiate widgets */
    builder->get_widget("window_preferences", window_);
    builder->get_widget("button_ok", button_ok_);
    builder->get_widget("button_about", button_about_);
    builder->get_widget("button_memory", button_memory_);
    builder->get_widget("button_cpu", button_cpu_);
    builder->get_widget("button_io", button_io_);
    builder->get_widget("check_notify", check_notify_);
    builder->get_widget("check_io", check_io_);

    /* assign default values */
    window_->set_icon_name("indicator-monitor");
    button_memory_->set_range (1.0, 5201314.0);
    button_memory_->set_increments (10.0, 10.0);
    button_memory_->set_value (config_.memory_valve);
    button_memory_->grab_focus ();
    button_cpu_->set_range (1.0, 100.0);
    button_cpu_->set_increments (1.0, 1.0);
    button_cpu_->set_value (config_.cpu_valve);
    button_io_->set_range (0.1, 5201314.0);
    button_io_->set_increments (5.0, 5.0);
    button_io_->set_value (config_.io_valve);
    check_notify_->set_active (config_.enable_notification);
    check_io_->set_active (config_.disable_io_notification);

    /* connect signals */
    button_ok_->signal_clicked().connect (
            mem_fun (*this, &preferences_dialog::on_button_close));
    button_about_->signal_clicked().connect (
            mem_fun (*this, &preferences_dialog::on_button_about));
    button_cpu_->signal_value_changed().connect (
            mem_fun (*this, &preferences_dialog::on_change_button_cpu));
    button_memory_->signal_value_changed().connect (
            mem_fun (*this, &preferences_dialog::on_change_button_memory));
    button_io_->signal_value_changed().connect (
            mem_fun (*this, &preferences_dialog::on_change_button_io));
    check_notify_->signal_toggled().connect (
            mem_fun (*this, &preferences_dialog::on_change_check_notify));
    check_io_->signal_toggled().connect (
            mem_fun (*this, &preferences_dialog::on_change_check_io));
}

void preferences_dialog::show () {
    window_->show_all ();
}

void preferences_dialog::on_change_button_memory () {
    config_.memory_valve = button_memory_->get_value();
}

void preferences_dialog::on_change_button_cpu () {
    config_.cpu_valve = button_cpu_->get_value ();
}

void preferences_dialog::on_change_button_io () {
    config_.io_valve = button_io_->get_value ();
}

void preferences_dialog::on_change_check_notify () {
    config_.enable_notification = check_notify_->get_active ();
}

void preferences_dialog::on_change_check_io () {
    config_.disable_io_notification = check_io_->get_active ();
}

void preferences_dialog::on_button_close () {
    window_->hide ();
}

void preferences_dialog::on_button_about () {
#undef LICENSE
#define LICENSE "This program is free software: you can redistribute it \nand/or modify it under the terms of the GNU General \nPublic License version 3, as published by the Free \nSoftware Foundation.\n\nThis program is distributed in the hope that it will be \nuseful, but WITHOUT ANY WARRANTY; without even \nthe implied warranties of MERCHANTABILITY, \nSATISFACTORY QUALITY, or FITNESS FOR A \nPARTICULAR PURPOSE.  See the GNU General Public \nLicense for more details.\n\nYou should have received a copy of the GNU General \nPublic License along with this program.  If not, see \n<http://www.gnu.org/licenses/>."
    Gtk::AboutDialog dialog;

    dialog.set_transient_for(*window_);
    dialog.set_name(_("System Load Indicator"));
    dialog.set_logo_icon_name("indicator-monitor");
    dialog.set_version(VERSION);
    dialog.set_copyright("Zhao Cheng, 2010");
    dialog.set_license(LICENSE);
    dialog.set_website("http://code.google.com/p/indicator-monitor/");
    dialog.set_comments(_("An indicator to show high-resource-using processes."));

    std::list<ustring> authors_container;
    authors_container.push_back ("Zhao Cheng <zcbenz@gmail.com>");
    dialog.set_authors(authors_container);

    dialog.run();
}
